package com.lishuang.system.controller;

import com.lishuang.basic.util.AjaxResult;
import com.lishuang.basic.util.PageList;
import com.lishuang.system.domain.DictionaryDetail;
import com.lishuang.system.query.DictionaryDetailQuery;
import com.lishuang.system.service.IDictionaryDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dictionaryDetail")
@Api(value = "数据字典类型的API", description = "数据字典类型相关的CRUD功能")
public class DictionaryDetailController {

    @Autowired
    private IDictionaryDetailService dictionaryDetailService;

    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询")
    public DictionaryDetail findOne(@PathVariable("id") Long id) {
        return dictionaryDetailService.loadById(id);
    }

    @GetMapping
    @ApiOperation(value = "查询所有")
    public List<DictionaryDetail> findAll() {
        return dictionaryDetailService.loadAll();
    }

    @PostMapping
    @ApiOperation(value = "分页查询")
    public PageList<DictionaryDetail> queryPage(@RequestBody DictionaryDetailQuery query) {
        return dictionaryDetailService.queryPage(query);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID删除数据")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            dictionaryDetailService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败");
        }

    }

    @PatchMapping
    @ApiOperation(value = "通过ids批量删除数据")
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
        try {
            dictionaryDetailService.batchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("批量删除失败");
        }

    }

    @PutMapping
    @ApiOperation(value = "添加或者修改(通过有无ID区分)")
    public AjaxResult addOrUpdate(@RequestBody DictionaryDetail dictionaryDetail) {
        try {
            if (dictionaryDetail.getId() == null) {
                dictionaryDetailService.save(dictionaryDetail);
            } else {
                dictionaryDetailService.update(dictionaryDetail);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }

}
